﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body style="margin:0px;padding:0px;">
    <h2>Regiter Test!!!!</h2>

    <div style="background-color:red;height:20px;width:100%;" id="lala"></div>

	<form id="userData">
		<label>Email</label>
		<input type="text" name="Email" placeholder="Email" />
		<br />
		<label>User Name</label>
		<input type="text" name="userName" placeholder="User Name" />
		<br />
		<label>Password</label>
		<input type="password" name="Password" placeholder="Password" />
		<br />
		<label>Comfirm Password</label>
		<input type="password" name="ConfirmPassword" placeholder="Comfirm Password" />
		<br />
		<label>Gender</label>
		<input type="radio" name="Gender" value="男" checked="checked" />男
		<input type="radio" name="Gender" value="女" />女
		<br />
		<label>Location</label>
		<input type="text" name="Location" placeholder="Location" />
		<br />
		<input type="submit" id="register" value="register" />
		<input type="submit" id="login" value="login" />
	</form>
	<form id="rf">
		<label>Id</label>
		<input type="text" name="Id" placeholder="Id" />
		<br />
		<label>Real Name</label>
		<input type="text" name="RealName" placeholder="RealName" />
		<br />
		<input type="submit" id="auth" value="auth" />
	</form>

    <input type="button" id="getUserInfo" value="User Info" />
	<input type="submit" id="getAllUsers" value="All users" />
	<input type="submit" id="fl" value="Get Followed" />
	<input type="submit" id="ll" value="All users" />
	<br />
    <input type="button" id="logout" value="logout" />

    <pre id="output">

    </pre>

    <script src="Scripts/jquery-1.10.2.js"></script>
    <script>
        $(function () {
            var acessToken = "";

            var getUserInfo = function () {
                var url = "/api/Account/UserInfo";
                $.ajax(url, {
                    type: "GET",
                    headers: getHeaders()
                }).always(showRespond);

                return false;
            }

            var auth = function () {
            	var url = "/api/Account/RealNameAuth";
            	$.ajax(url, {
            		type: "POST",
            		data: $("#rf").serialize(),
            		headers: getHeaders()
            	}).always(showRespond);

            	return false;
            }

            var showRespond = function (object) {
                $("#output").text(JSON.stringify(object, null, 4));
            }

            var saveAccessToken = function (data) {
                acessToken = data.access_token;
            }

            var register = function () {
                var url = "/api/Account/Register";
                var data = $("#userData").serialize();
                $.post(url, data);

                return false;
            }

            var login = function () {
                var url = "/Token";
                var data = $("#userData").serialize();
                data = data + "&RememberMe=true&grant_type=password";
                $.post(url, data)
                    .success(saveAccessToken)
                    .always(showRespond);

                return false;
            }

            var getHeaders = function () {
                if (acessToken) {
                    return { "Authorization": "Bearer " + acessToken };
                }
            }

            var logout = function () {
                var url = "/api/Account/Logout";
                $.post(url)
                    .success(successLogout);
                return false;
            }

            var successLogout = function () {
                alert("~~~");
                acessToken = "";
            }

            var getAllUsers = function () {
            	var url = "/api/Account/AllUsers";
            	$.ajax(url, {
            		type: "GET",
            		headers: getHeaders()
            	}).always(showRespond);

                return false;
            }

            var fl = function () {
            	var url = "/api/Account/FollowedList";
            	$.ajax(url, {
            		type: "GET",
            		headers: getHeaders()
            	}).always(showRespond);

            	return false;
            }

            $("#register").click(register);
            $("#login").click(login);
            $("#logout").click(logout);
            $("#getUserInfo").click(getUserInfo);
            $("#auth").click(auth);
            $("#getAllUsers").click(getAllUsers);
            $("#fl").click(fl);
        });
    </script>

    <!--<script>
        $(docunmment).ready(function () {
            var w = screen.availWidth;
            $("#lala").css("width:" + w);
        });
    </script>-->
</body>
</html>
